﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tasker.Composition;
using Tasker.Infrastructure.Entities;
using Tasker.Infrastructure.Repositories;
using Tasker.Infrastructure.Repositories.Base;
using Tasker.Repositories.Entities;

namespace Tasker.Repositories
{
    public class StatusRepositoryEF: IStatusRepository
    {
        private EntitiesContext _context = Resolver.Resolve<EntitiesContext>();
        private IContextManager _manager = Resolver.Resolve<IContextManager>();

        public IStatus GetStartStatus()
        {
            IStatus status = _context.Status.SingleOrDefault(st => st.Id == 1);
            return status;
        }

        public IStatus SelectById(int id)
        {
            return _context.Status.SingleOrDefault(status => status.Id == id);
        }

        public IEnumerable<IStatus> SelectAll()
        {
            return _context.Status.ToList();
        }

        public void Update(IStatus entity)
        {
            Status res = _context.Status.SingleOrDefault(status => status.Id == entity.Id);
            res.Name = entity.Name;

            _manager.Save();
        }

        public IStatus Create()
        {
            return new Status();
        }

        public void Delete(IStatus entity)
        {
            Status st = _context.Status.SingleOrDefault(status => status.Id == entity.Id);
            _context.Status.DeleteObject(st);
        }

        public IStatus Insert(IStatus entity)
        {
            Status res = entity as Status;
            _context.Status.AddObject(res);
            _manager.Save();
            return res;
        }
    }
}
